<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="ln"></div>
    <script src="./src/react.js"></script>
    <script src="./src/react-dom.js"></script>
    <script src="./src/babel.js"></script>
    <script type="text/babel">
        class C extends React.Component {
            constructor() {
                super();
                this.state = {
                    list: [
                        { id: 1, user: 'jmqs', age: 16 },
                        { id: 2, user: 'jsln', age: 15 },
                        { id: 3, user: 'ks', age: 18 },
                        { id: 4, user: 'wtl', age: 17 },
                    ],
                    user: '1',
                    age: '2',
                    editId: '',
                    flag: true
                }
            }
            changeInput = (event) => {
                this.setState({
                    [event.target.name]: event.target.value
                })
            }
            add = () => {
                let obj = { id: +new Date(), user: this.state.user, age: this.state.age }
                let list = JSON.parse(JSON.stringify(this.state.list));

                list.push(obj);

                this.setState({
                    list: list
                })

            }
            del = (id) => {
                let list = JSON.parse(JSON.stringify(this.state.list))
                let index = list.findIndex(v => v.id == id)
                list.splice(index, 1)
                this.setState({
                    list: list
                })
            }
            edit = (id) => {
                let list = JSON.parse(JSON.stringify(this.state.list));
                let obj = list.find(v => v.id == id);

                this.setState({
                    user: obj.user,
                    age: obj.age,
                    editId: obj.id,
                    flag: false
                })
            }
            setedit = () => {
                let list = JSON.parse(JSON.stringify(this.state.list));
                let index = list.findIndex(v => v.id == this.state.editId);
                list[index].user = this.state.user;
                list[index].age = this.state.age;

                this.setState({
                    list: list,
                    flag: true
                })

            }
            render() {
                return (
                    <div>
                        <form>
                            <div>
                                <input type="text" name="user" value={this.state.user} onChange={this.changeInput} />
                            </div>
                            <div>
                                <input type="text" name="age" value={this.state.age} onChange={this.changeInput} />
                            </div>
                            <div>
                                {
                                    this.state.flag ? <input type="button" value="提交" onClick={this.add} /> : <input type="button" value="确认修改" onClick={this.setedit} />
                                }
                            </div>
                        </form>
                        {/* 展示表单 */}
                        <table border="1">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <th>年龄</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    this.state.list.length > 0 ? this.state.list.map(v => {
                                        return (
                                            <tr key={v.id}>
                                                <td>{v.user}</td>
                                                <td>{v.age}</td>
                                                <td>
                                                    <button onClick={() => this.del(v.id)}>删除</button>
                                                    <button onClick={() => this.edit(v.id)}>修改</button>

                                                </td>
                                            </tr>
                                        )
                                    }) : <tr><th colSpan="3">暂无数据</th></tr>
                                }
                            </tbody>
                        </table>
                    </div>
                )
            }
        }

        ReactDOM.render(
            <div>
                <C></C>
            </div>,
            document.getElementById('ln')
        )
    </script>
</body>

</html>